<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <router-link to="/home">主页</router-link>
        <router-link to="/introduce">公司介绍</router-link>
        <router-link to="/profile">产品详情</router-link>

        <router-view></router-view>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>
    let Home = {
        template: `<h1>主页的内容</h1>`
    }
    let Introduce = {
        template: `<h1>公司介绍</h1>`
    }
    let Profile = {
        template: `<h1>产品详情</h1>`
    }


    let routes = [{
        path: '/home',
        component: Home
    }, {
        path: '/introduce',
        component: Introduce
    }, {
        path: '/profile',
        component: Profile
    }, {
        path: "/login",
        component: () => Promise.resolve({
            template: `<h1>登录</h1>`
        })
    }]

    let router = new VueRouter({
        routes
    })
    // let token = "KhJBAv9UgHvgJYoiibjQjqDHsDYeakTO"
    let token = "1111";
    //设置token的值，下面的命令就是设置一个全局守卫
    router.beforeEach((to, from, next) => {
        console.log("现在要前往路由", to);
        console.log("当前所在的路由的信息", from);
        // next();// 表示运行导航的变化
        // next(false);// 表示中断 当前导航
        if (!token && to.path != "/login") {
            // if(!token){
            next("/login");
            // 如果token是空的，并且前往的位置不是"/login"地址
        } else {
            next();
        }
    })

    let vm = new Vue({
        el: "#app",
        router,
    })
</script>

</html>